package com.milo.coding.atomic.unsafe;

import com.milo.coding.util.UnsafeInstance;
import sun.misc.Unsafe;

/**
 * @Author milo
 * @Date 2021-02-02 18:28:29
 * @Description 跨方法加锁
 */
public class ObjectMonitorRunner {
    static Object object = new Object();
    static Unsafe unsafe = UnsafeInstance.reflectGetUnsafe();

    public static void method1(){
        unsafe.monitorEnter(object);
    }

    public static void method2(){
        unsafe.monitorExit(object);
    }

    public static void main(String[] args) {
        // jvm内置锁
        synchronized (object){

            //写逻辑
        }

        // 加锁
        method1();

        // 写逻辑

        // 解锁
        method2();
    }

}
